import {_decorator, CCInteger, Component} from 'cc';

const {ccclass, property} = _decorator;

/**
 * 背景控制器
 */
@ccclass('BackgroundController')
export class BackgroundController extends Component {
    /**
     * 速度
     */
    @property(CCInteger)
    public speed: number = 1;
    /**
     * 范围
     */
    @property(CCInteger)
    public range: number = 20;

    start() {

    }

    update(deltaTime: number) {
        for (let child of this.node.children) {
            // 移动
            child.setPosition(child.position.x, child.position.y, child.position.z + this.speed * deltaTime);
            // 当对象移除屏幕时
            if (child.position.z > this.range) {
                // 重置设置位置
                child.setPosition(child.position.x, child.position.y, child.position.z - this.range * 2);
            }
        }
    }
}